

#include "taskoptions.h"

#undef String

namespace freedatabase
{
	string utf82gbk2(char *ori)
	{
		FD::Kernel::FdMultiByteString::CharSet cs = FD::Kernel::FdMultiByteString::CS_UTF8;
		FD::Kernel::FdGbkString gbkName(ori, cs);
		return gbkName.c_str();
	}


	char *parsechar(Local<v8::String> arg)
	{
		int len = arg->Utf8Length();
		char *charparam = new char[len + 1];
		arg->WriteUtf8(charparam, len + 1);
		return charparam;
	}

	Persistent<Function> TaskOptions::constructor;

	TaskOptions::TaskOptions()
	{
	}

	TaskOptions::~TaskOptions()
	{
	}

	void TaskOptions::Init(v8::Local<v8::Object> exports)
	{
		Isolate *isolate = exports->GetIsolate();

		Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
		tpl->SetClassName(v8::String::NewFromUtf8(isolate, "TaskOptions"));
		tpl->InstanceTemplate()->SetInternalFieldCount(1);

		NODE_SET_PROTOTYPE_METHOD(tpl, "setTaskName", setTaskName);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setTaskID", setTaskID);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setParentTaskID", setParentTaskID);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setPlanS", setPlanS);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setPlanE", setPlanE);

		NODE_SET_PROTOTYPE_METHOD(tpl, "setActualS", setActualS);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setActualE", setActualE);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setTaskType", setTaskType);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setGuid", setGuid);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setTotalPrice", setTotalPrice);

		NODE_SET_PROTOTYPE_METHOD(tpl, "setDesc", setDesc);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setBActive", setBActive);
		NODE_SET_PROTOTYPE_METHOD(tpl, "setPercent", setPercent);

		constructor.Reset(isolate, tpl->GetFunction());
		exports->Set(v8::String::NewFromUtf8(isolate, "TaskOptions"), tpl->GetFunction());
	}
	void TaskOptions::New(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();

		if (args.IsConstructCall())
		{
			TaskOptions *connOpts = new TaskOptions();
			connOpts->Wrap(args.This());
			args.GetReturnValue().Set(args.This());
		}
		else
		{
			Local<Function> cons = Local<Function>::New(isolate, constructor);
			args.GetReturnValue().Set(cons->NewInstance());
		}
	}

	void TaskOptions::setTaskID(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsNumber())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());
		connOpts->task.taskID = args[0]->NumberValue();
	}

	void TaskOptions::setTaskName(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());
		connOpts->task.taskName = utf82gbk2(parsechar(args[0]->ToString()));
	}
	void TaskOptions::setParentTaskID(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsNumber())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.parentTaskID = args[0]->NumberValue();
	}

	void TaskOptions::setPlanS(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.planS.fromString(utf82gbk2(parsechar(args[0]->ToString())).c_str());
	}

	void TaskOptions::setPlanE(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.planE.fromString(utf82gbk2(parsechar(args[0]->ToString())).c_str());
	}

	void TaskOptions::setActualS(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.actualS.fromString(utf82gbk2(parsechar(args[0]->ToString())).c_str());
	}

	void TaskOptions::setActualE(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.actualE.fromString(utf82gbk2(parsechar(args[0]->ToString())).c_str());
	}

	void TaskOptions::setTaskType(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsNumber())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.taskType = EnumTaskType((int)(args[0]->NumberValue()));
	}

	void TaskOptions::setGuid(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.guid = utf82gbk2(parsechar(args[0]->ToString())).c_str();
	}

	void TaskOptions::setTotalPrice(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsNumber())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.totalPrice = args[0]->NumberValue();
	}

	void TaskOptions::setDesc(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsString())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.desc = utf82gbk2(parsechar(args[0]->ToString())).c_str();
	}

	void TaskOptions::setBActive(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsBoolean())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.bActive = args[0]->BooleanValue();
	}

	void TaskOptions::setPercent(const FunctionCallbackInfo<Value> &args)
	{
		Isolate *isolate = args.GetIsolate();
		if (args.Length() < 1)
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong number of arguments")));
			return;
		}
		if (!args[0]->IsNumber())
		{
			isolate->ThrowException(Exception::TypeError(v8::String::NewFromUtf8(isolate, "wrong argument type")));
			return;
		}
		TaskOptions *connOpts = ObjectWrap::Unwrap<TaskOptions>(args.Holder());

		connOpts->task.percent = args[0]->NumberValue();
	}

} // namespace freedatabase
